﻿/*************************************
Function：文件，压缩处理基类
 Version：1.0
    Date：2012-05-08
  Author：rjdong
   Email：ooxxab@gmail.com
 WebSite：http://emake.info
*************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Yahoo.Yui.Compressor.MsBuildTask;
using Yahoo.Yui.Compressor;
using System.Globalization;
using Wind.Compressor.Entity;

namespace Wind.Compressor.Business
{
    public abstract class BaseCompressor
    {
        #region Yahoo.Yui.Compress Mothed
        /// <summary>
        /// Css压缩
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string CssCompress(string str)
        {
            return CssCompressor.Compress(str);
        }
        /// <summary>
        /// Css压缩
        /// </summary>
        /// <param name="str">css</param>
        /// <param name="columnWidth">specified column width ,for nice reading </param>
        /// <param name="cssCompressionType">压缩类型</param>
        /// <param name="removeComments">是否删除注释</param>
        /// <returns></returns>
        public string CssCompress(string css, int columnWidth, CssCompressionType cssCompressionType, bool removeComments)
        {
            return CssCompressor.Compress(css, columnWidth, cssCompressionType, removeComments);
        }
        /// <summary>
        /// Js压缩
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string JsCompress(string str)
        {
            return JavaScriptCompressor.Compress(str);
        }
        /// <summary>
        /// Js压缩
        /// </summary>
        /// <param name="javaScript">js</param>
        /// <param name="isVerboseLogging">长日志</param>
        /// <param name="isObfuscateJavascript">混淆</param>
        /// <param name="preserveAllSemicolons">保持所有符号</param>
        /// <param name="disableOptimizations">禁用优化</param>
        /// <param name="lineBreakPosition">折行位置</param>
        /// <param name="encoding">编码</param>
        /// <param name="threadCulture">本地化</param>
        /// <param name="isEvalIgnored">忽略Eval</param>
        /// <param name="compressionType">压缩类型</param>
        /// <returns></returns>
        public static string JsCompress(string javaScript,
            bool isVerboseLogging,
            bool isObfuscateJavascript,
            bool preserveAllSemicolons,
            bool disableOptimizations,
            int lineBreakPosition,
            Encoding encoding,
            CultureInfo threadCulture,
            bool isEvalIgnored,
            JavaScriptCompressionType compressionType)
        {
            return JavaScriptCompressor.Compress(javaScript,
                isVerboseLogging,
                isObfuscateJavascript,
                preserveAllSemicolons,
                disableOptimizations,
                lineBreakPosition,
                encoding,
                threadCulture,
                isEvalIgnored,
                compressionType);
        }
        #endregion

        /// <summary>
        /// 单个文件压缩
        /// </summary>
        /// <param name="fullFileName">文件名，用于自动保存</param>
        /// <param name="strContent">（仅压缩strContent）</param>
        /// <param name="setting"></param>
        /// <param name="destPath"></param>
        /// <returns></returns>
        public string DoCompress(string fullFileName, string strContent, SettingInfo setting, string destPath)
        {
            string result = string.Empty;
            try
            {
                if (!destPath.EndsWith("\\"))
                    destPath += "\\";

                string name = Path.GetFileNameWithoutExtension(fullFileName);
                string ext = Path.GetExtension(fullFileName).ToLower();

                if (ext == ".css")
                {
                    int CssColumnWidth = 0;
                    if (setting.CssIsColumnWidth)
                        CssColumnWidth = setting.CssColumnWidth;
                    result = CssCompress(strContent, CssColumnWidth, setting.CssCompressionType, setting.CssRemoveComments);
                    if (setting.CssIsAutoSave)
                    {
                        if (setting.CssIsAutoSave)
                            name += setting.CssAutoSaveEndStr;

                        if (setting.CssIsVersion)
                            name += setting.CssVersion;

                        Utils.WriteFile(destPath + name + ext, result, Utils.GetEncoding(setting.CssEncodeName));
                    }
                }
                else if (ext == ".js")
                {
                    int jsLineBreakPosition = 0;
                    if (setting.JsIsLineBreakPosition)
                        jsLineBreakPosition = setting.JsLineBreakPosition;

                    result = JsCompress(strContent, setting.JsIsVerboseLogging,
                        setting.JsIsObfuscateJavascript,
                        setting.JsPreserveAllSemicolons,
                        setting.JsDisableOptimizations,
                        jsLineBreakPosition,
                        Encoding.UTF8,
                        CultureInfo.CurrentCulture,
                        setting.JsIsEvalIgnored,
                        setting.JsCompressionType
                        );

                    if (setting.JsIsAutoSave)
                    {
                        if (setting.JsIsAutoSave)
                            name += setting.JsAutoSaveEndStr;

                        if (setting.JsIsVersion)
                            name += setting.JsVersion;

                        Utils.WriteFile(destPath + name + ext, result, Utils.GetEncoding(setting.JsEncodeName));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.Business.BaseWindCompressor.Compress error", ex);
            }
            return result;
        }   
    }
}
